/***** PARAMETROS INICIAIS *****/

var windowParams = {  
	label: '',
	url: '',
	data: '',
	existModal: false,
	loaded: false,
	validationURL: null,
	
	setLabel: function(label) { this.label = label; },
	getLabel: function() { return this.label; },
	setURL:   function(url) { this.url = url; },
	getURL:   function() { return this.url; },
	setData: function(data) { this.data = data; }, 
	getData: function() { return this.data; },
	setExistModal: function(existModal) { this.existModal = existModal; }, 
	getExistModal: function() { return this.existModal; },
	setLoaded: function(loaded) { this.loaded = loaded; }, 
	getLoaded: function() { return this.loaded; },
	setValidationURL: function(validationURL) { this.validationURL = validationURL; },
	getValidationURL: function() { return this.validationURL; }
};

/***** JQUERY *****/

jQuery(function($) {
//	$('#actionHeaderAdd').click(function() {
//		$('#defaultForm').cleanForm();
//	});
	
	initialize();
	
	if(windowParams.getExistModal()){
		$('#tableData').hide();
	}
	
	$('.cancel').click(function() {
		$('#defaultForm').cleanForm();
		return false;
	});
});

/***** METODOS *****/

jQuery.fn.extend({ 
	removeAjax: function(url) {
		var form = this;
		$('.remove').click(function() {
			$.ajax({url: url, type: $(form).attr('method'), data: $(form).serialize(), dataType: 'json'})
			.done(function(data){ 
				addFeedbackNotifications(data); 
				$(form).cleanForm(); 
				initialize();
			})
		    .fail(function(data){ 
		    	addFeedbackNotifications(data);
		    });
			return false;
		});
	},
	removeTableAjax: function(url) {
		var form = this;
		$('a.removeTable').live("click", function() {
			$.ajax({url : url + $(this).attr('value')+'/remover', type: 'GET', dataType: 'json',})
			.done(function(data){ 
				addFeedbackNotifications(data); 
				$(form).fillForm(data);
			})
		    .fail(function(data){ 
		    	addFeedbackNotifications(data);
		    });
			return false;
		});
	},
	getAjax: function(url, subData){
		$('li.item').live("click", function() {
			$.ajax({url: url + $(this).find('a').attr('id') + '/dados.json', dataType: 'json',
				success: function(data){ 
					addFeedbackNotifications(data); 
					$('#defaultForm').fillForm(data);
					valoresEdit();
				},
				error: function(data){ 
					addFeedbackNotifications(data);
				}
			});
		});
	},
	submitAjax: function(url){
		var form = this;
		$(form).submit(function() {
			form.find(':input:disabled').removeAttr('disabled'); // habilita os inputs para que os valores possam ser recuperados e salvos
			$.ajax({url: url, type: $(form).attr('method'), data: $(form).serialize(), dataType: 'json',})
			.done(function(data){
				addFeedbackNotifications(data); 
				$(form).cleanForm(); 
				initialize();
			})
		    .fail(function(data){ 
		    	addFeedbackNotifications(data);
		    });			
			return false;
		});
	},
	saveAjax: function(url){
		var form = this;
		$.ajax({url: url, type: $(form).attr('method'), data: $(form).serialize(), dataType: 'json',})
		.done(function(data){
			addFeedbackNotifications(data);
			$(form).fillForm(data);
			if(windowParams.getExistModal()){$('#windowDialog').fecharModal();}	
		})
		.fail(function(data){ 
		    addFeedbackNotifications(data);
		    if(windowParams.getExistModal()){$('#windowDialog').fecharModal();}	
		});			
		return false;
	},
	validateAjax: function(){
		$('.ajaxValidate').change(function() {
			if(windowParams.validationURL == null) {
				var form = $(this).parents('form');
				var serializedForm = $(form).serializeArray();
				serializedForm.push({name: "field", value: $(this).attr('name')});
				serializedForm = jQuery.param(serializedForm);
				
				$.ajax({url : windowParams.validationURL, type: $(form).attr('method'), data: serializedForm, dataType: 'json'})
				.done(function(data){ 
					addFeedbackNotifications(data); 
				})
			    .fail(function(data){ 
			    	addFeedbackNotifications(data); 
			    });
			}
			return false;
		});
	},
	cleanForm: function(){
		if(windowParams.getExistModal()){$('#tableData').hide();}		
		$(this).find(':text').val('');
		$(this).find(':hidden').val('');
		$(this).find(':selected').val('');
		$('li.item').removeClass('selected');
		$('span.error').remove();		
	},
	fillForm: function(data){
		$(this).cleanForm();
		if(windowParams.getExistModal()){$('#tableData').show();}		
		$('span.error').remove();
		$('li.item').find('a#' + data.entity.id).parent().addClass('selected');
		$.each(data.entity, function(name, value) {
			$('[name="entity.'+name+'"]').val(value);
		});
		
		// Metodo criado no JS de cada tela para montar as tags para os subdados
		fillFormData(data);
	},
	fecharModal: function(){
		$(this).modal('hide'); 
	}
});	

/***** FUNCOES *****/

function initialize() {
	if(windowParams.loaded) { 
		$.ajax({url : windowParams.getURL(), dataType: 'json', async: false,})
		.done(function(data) { 
			windowParams.setData(data); 
		}); 
	}
	
	if (windowParams.getData() != null && windowParams.getData().list != null) {
		var ul = $('#itemListItems').find('ul');
		$(ul).html('');
		$.each(windowParams.getData().list, function(key, value) {
			var template = '<li class="item"><a class="person" id="'+ value['id'] + '">' + value[windowParams.getLabel()] + '</a></li>';
			$(ul).append(template);
		});
	}
	windowParams.loaded = true;
	
	// Metodo criado no JS de cada tela para montar as setar valores default
	getDropDown();
	valoresDefault();
}